<?php
/**
 * (logic)
 */
namespace App\Logic\Operation;

use App\Models\Basics\AmazonCountry;
use App\Models\Basics\ExchangeRate;
use App\Models\Basics\SysICountry;
use App\Models\Basics\Transport;
use App\Models\Operation\ProfitSku;
use App\Models\Product\ProductAmazonSku;
use App\Models\Product\ProductPacking;
use App\Models\Product\ProductSku;
use Exception;
class SyncPS
{
    /**
     * 同步数据（sku产品利润报表）
     * 设定：每天运行
     */
    public static function exe()
    {
        $pas = ProductAmazonSku::query()
            ->where('status',ProductAmazonSku::STATUS_YES)
            ->select('country_id','psku_id')
            ->distinct()
            ->get();

        $new = [];
        //先处理数据，countryId_pSkuId
        foreach($pas as $_pas){
            $value = $_pas->country_id.'_'.$_pas->psku_id;
            if(!in_array($value,$new)){
                $new[] = $value;
            }
        }
        $new = $new ? array_unique($new) : [];
        $count = count($new);
        if($count < 4000){
            report(new Exception("Failed to artisan sync-ps : count(" . $count . ")总数异常",122));
            die(0);
        }
        $ps = ProfitSku::query()->select('country_id','psku_id')->distinct()->get();
        $old = [];
        //先处理数据，countryId_pSkuId
        foreach($ps as $_ps){
            $value = $_ps->country_id.'_'.$_ps->psku_id;
            if(!in_array($value,$old)){
                $old[] = $value;
            }
        }
        $old = $old ? array_unique($old) : [];
        //需要删除的数据
        $delete = array_diff($old,$new);
        foreach($delete as $_delete){
            list($countryId,$pSkuId) = explode('_',$_delete);
            $count = ProductAmazonSku::query()
                ->where('status',1)
                ->where('country_id',$countryId)
                ->where('psku_id',$pSkuId)
                ->count();
            if($count > 0)continue;
            ProfitSku::query()->where('country_id',$countryId)->where('psku_id',$pSkuId)->delete();
        }
        //新增数据
        $add = array_diff($new,$old);
        $area = [];
        $pasArr = [];
        $skuArr = [];
        $price = [];
        $vat = [];
        $transportArr = [];
        $rateOddsArr = [];
        foreach($add as $_add){
            list($countryId,$pSkuId) = explode('_',$_add);
            $count = ProfitSku::query()
                ->where('country_id',$countryId)
                ->where('psku_id',$pSkuId)
                ->count();
            if($count > 0)continue;
            if(!array_key_exists($_add,$pasArr)){
                $pasArr[$_add] = ProductAmazonSku::query()->where('status',1)->where('country_id',$countryId)->where('psku_id',$pSkuId)->first();
            }
            $pasModel = $pasArr[$_add];
            if(!$pasModel)continue;
            if(!array_key_exists($pSkuId,$skuArr)){
                $skuArr[$pSkuId] = ProductSku::getModelById($pSkuId);
            }
            $sku = $skuArr[$pSkuId];
            if(!array_key_exists($countryId,$area)){
                $area[$countryId] = SysICountry::getAreaIdByCountryId($countryId);
            }
            $areaId = $area[$countryId];
            if(!array_key_exists($countryId,$rateOddsArr)){
                $currencyCode = AmazonCountry::getCurrencyCodeByCountryId($countryId);
                $rateOddsArr[$countryId] = ExchangeRate::getRateOddsToRMB($currencyCode,time());
            }
            $rateOdds = $rateOddsArr[$countryId];
            if(!array_key_exists($pSkuId,$price)){
                $price[$pSkuId] = ProductSku::getPuPriceNoTax($sku);
            }
            $tplRMB = $price[$pSkuId];
            if(!array_key_exists($countryId,$vat)){
                $vat[$countryId] = AmazonCountry::getVatByCountryId($countryId);
            }
            $_vat = $vat[$countryId];
            $shipWeight = ProductPacking::getShipWeight($pSkuId);
            if(!array_key_exists($areaId,$transportArr)){
                $transportArr[$areaId] = Transport::getTransportFee($areaId);
            }
            $transport = $transportArr[$areaId];

            //sku产品利润
            ProfitSku::addProfitSku($pasModel,$rateOdds,$tplRMB,$shipWeight,$transport->air_fee ?? 0,$transport->sea_fee ?? 0,$_vat);
        }
    }
}
